cc.Class({
    extends: cc.Component,

    properties: {
        // 3 action buttons
        mBtnAction: [cc.Node],
        mBtnPlayerInfo: [cc.Node],
        mMainView: cc.Node,
        mSelectPlayerView: cc.Node,
        // scroll view
        // mRichText: cc.RichText,
        mSelHeroPrefab: cc.Prefab,
        mScrollContent: cc.Node,
    },

    // LIFE-CYCLE CALLBACKS:
    start () {

    },

    // update (dt) {},

    onLoad () {
        // set prefab
        this.heroData = [];
        this.heroData[0] = { id: 0, style: 0 };
        this.heroData[1] = { id: 1, style: 1 };

        for (let i = 0; i < this.heroData.length; ++i) {
            const hero = cc.instantiate(this.mSelHeroPrefab);
            this.mScrollContent.addChild(hero);
            // 获取预制件
            const prefab = hero.getComponent('SelHeroPrefab');
            prefab.setStyle(this.heroData[i]);
        }

        this.mMainView.setPosition(0, 0);
        this.mSelectPlayerView.active = false;
        this.btnStartPos = [];
        cc.log('record position');
        for (let i = 0; i <  this.mBtnAction.length; ++i) {
            this.btnStartPos[i] = this.mBtnAction[i].getPosition();
        }

        // 坐骑、角色、宠物
        for (let i = 0; i < this.mBtnPlayerInfo.length; ++i) {
            const info = this.mBtnPlayerInfo[i];
            // 播放pressed
            info.getChildByName('Pressed').active = false;
            info.on(cc.Node.EventType.TOUCH_START, this.touchShowPressed, this);
            info.on(cc.Node.EventType.TOUCH_END, this.touchHidePressed, this);
            info.on(cc.Node.EventType.TOUCH_CANCEL, this.touchHidePressed, this);
        }
    },

    // show pressed picture
    touchShowPressed: function(target) {
        // 第一个 target 是信号，第二个target 是节点
        target.target.getChildByName('Pressed').active = true;
    },

    // hide pressed picture
    touchHidePressed: function(target) {
        target.target.getChildByName('Pressed').active = false;
    },

    loadGameScene() {
        cc.log('loading game scene');
        cc.director.loadScene('GameScene');
    },

    // open main view
    onOpenMainView() {
        cc.log('open main view');
        this.mMainView.active = true;
        this.mSelectPlayerView.active = false;
    },

    // open select player view
    onOpenSelectPlayerView() {
        cc.log('open select player view');
        this.mMainView.active = false;
        this.mSelectPlayerView.active = true;
    },

    // 按钮淡入淡出效果
    onActionBtnOutOrIn: function (isOut) {
        for (let i = 0; i < this.mBtnAction.length; ++i) {
            let delayTime = cc.delayTime(0.1*i);
            let moveTo;
            if (isOut) {
                moveTo = cc.moveTo(0.3, cc.v2(350, this.btnStartPos[i].y));
            } else {
                moveTo = cc.moveTo(0.3, this.btnStartPos[i]);
            }
            const seq = cc.sequence(delayTime, moveTo);
            this.mBtnAction[i].runAction(seq);
        }
    }
});
